package com.luoxiang.socialandroid.dialog

import android.content.Context
import android.content.Intent
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import com.luoxiang.socialandroid.R
import com.luoxiang.socialandroid.activity.WebloadActivity
import com.luoxiang.socialandroid.databinding.DialogPrivacyBinding
import com.lxj.xpopup.impl.FullScreenPopupView

class PrivacyDialog(context: Context) : FullScreenPopupView(context) {
    private lateinit var mViewBinding: DialogPrivacyBinding
    private var onConfirmClickListener: (() -> Unit)? = null
    private var onCancleClickListener: (() -> Unit)? = null
    private val serviceAgreement = context.getString(R.string.mi_yonghuxieyi_1)
    private val userAgreement = context.getString(R.string.mi_yinsixieyi2)
    private val phoneAgreement = context.getString(R.string.mi_zhongguoyidongyinsixieyi2)
    val text =
        "觅聊重视并致力于保障您的个人隐私，我们根据监管要求更新了${serviceAgreement}和${userAgreement}，特别说明如下：\n1.为更好的帮您找到心仪的朋友，" +
                "会根据您设置的择偶条件向您做推荐；\n2.为了查看附近的用户，我们需要使用您的位置信息，您可以随时开启或关闭此项授权；\n3.您可以随时访问、" +
                "更正、删除您的个人信息，我们也提供了注销和反馈的渠道；\n4.未经您同意，我们不会从第三方获取、共享或向其提供您的信息；\n5.点击同意即表示您已阅读并同意全部条款。" +
                "我们非常重视您的个人信息保护。关于个人信息收集和使用的详细信息，你可以点击${phoneAgreement}${serviceAgreement}和${userAgreement}进行了解。";
    val serviceAgreementIndex = text.indexOf(serviceAgreement)
    val serviceAgreementEnd = serviceAgreementIndex + serviceAgreement.length
    val privatePrivacyIndex = text.indexOf(userAgreement)
    val privatePrivacyEnd = privatePrivacyIndex + userAgreement.length
    override fun getImplLayoutId() = R.layout.dialog_privacy
    override fun onCreate() {
        super.onCreate()
        val serviceAgreementClick = object : ClickableSpan() {
            override fun onClick(widget: View) {
                //用户协议
                goAgreement()
            }
        }
        val privatePrivacyClick = object : ClickableSpan() {
            override fun onClick(widget: View) {
                //隐私协议
                goPrivacy()
            }
        }

        mViewBinding = DialogPrivacyBinding.bind(popupImplView)
        mViewBinding.tvContentpricy.text = text
        val style = SpannableStringBuilder(text)
        style.setSpan(
            serviceAgreementClick,
            serviceAgreementIndex,
            serviceAgreementEnd,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        style.setSpan(
            privatePrivacyClick,
            privatePrivacyIndex,
            privatePrivacyEnd,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        mViewBinding.textView13.setOnClickListener {
            onConfirmClickListener?.invoke()
        }
        mViewBinding.textView15.setOnClickListener {
            onCancleClickListener?.invoke()
        }
        mViewBinding.tvContentpricy.movementMethod = LinkMovementMethod.getInstance()
        mViewBinding.tvContentpricy.text = style
    }
    fun setOnConfirmClickListener(onConfirmClickListener: () -> Unit) {
        this.onConfirmClickListener = onConfirmClickListener
    }
    fun setOnCancleClickListener(onCancleClickListener: () -> Unit) {
        this.onCancleClickListener = onCancleClickListener
    }


    //用户协议
    private fun goAgreement() {
        val intent = Intent(context, WebloadActivity::class.java)
        intent.putExtra("title", "用户协议")
        intent.putExtra("webUrl", "http://mlhtml-test.dhxwlkj.com/userAgreement/index.html")
        context.startActivity(intent)
    }

    //隐私协议
    private fun goPrivacy() {
        val intent = Intent(context, WebloadActivity::class.java)
        intent.putExtra("title", "隐私协议")
        intent.putExtra("webUrl", "http://mlhtml-test.dhxwlkj.com/privacyPolicy/index.html")
        context.startActivity(intent)
    }
}